package org.opentcs.kernel.controlcenter;

import java.util.Objects;
import java.util.TreeSet;
import java.util.logging.Logger;
import javax.swing.JDialog;
import org.opentcs.access.Kernel;

/**
 * A dialog for the user to choose a model to load and the mode the kernel
 * should be set to.
 * 
 * @author Philipp Seifert (Fraunhofer IML)
 */
public class ChooseModelDialog
    extends JDialog {

  /**
   * This class's logger.
   */
  private static final Logger log =
      Logger.getLogger(ChooseModelDialog.class.getName());
  /**
   * The name of the model the user has chosen.
   */
  private String modelName;
  /**
   * Flag whether user wants to stay in modeling mode or not.
   */
  private boolean modelingMode = true;

  /**
   * Creates new ChooseModelDialog.
   * 
   * @param kernel The kernel providing the model list.
   */
  public ChooseModelDialog(Kernel kernel) {
    initComponents();
    setLocationRelativeTo(null);

    Objects.requireNonNull(kernel);
    Object[] models = new TreeSet<>(kernel.getModelNames()).toArray();
    for (Object object : models) {
      modelsComboBox.addItem(object);
    }
    if (models.length == 0) {
      log.warning("No models available to choose from.");
      dispose();
    }
  }

  /**
   * Returns the name of the model the user has chosen.
   * 
   * @return modelName The name of the model.
   */
  public String getModelName() {
    return modelName;
  }

  /**
   * Returns if the user wants to stay in modeling mode.
   * 
   * @return True if yes, false otherwise.
   */
  public boolean modelingSelected() {
    return modelingMode;
  }

  // CHECKSTYLE:OFF
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    buttonGroup1 = new javax.swing.ButtonGroup();
    jPanel1 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();
    modelsComboBox = new javax.swing.JComboBox<Object>();
    radioButtonPanel = new javax.swing.JPanel();
    modellingRadioButton = new javax.swing.JRadioButton();
    operatingRadioButton = new javax.swing.JRadioButton();
    jPanel3 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    buttonPanel = new javax.swing.JPanel();
    okButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/opentcs/kernel/controlcenter/Bundle"); // NOI18N
    setTitle(bundle.getString("ChooseModelTitle")); // NOI18N
    setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
    setResizable(false);

    jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.PAGE_AXIS));

    jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ChooseModel"))); // NOI18N
    jPanel2.setLayout(new java.awt.BorderLayout());

    modelsComboBox.setPreferredSize(new java.awt.Dimension(53, 22));
    jPanel2.add(modelsComboBox, java.awt.BorderLayout.CENTER);

    jPanel1.add(jPanel2);

    radioButtonPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ChooseKernelMode"))); // NOI18N
    radioButtonPanel.setLayout(new java.awt.GridBagLayout());

    buttonGroup1.add(modellingRadioButton);
    modellingRadioButton.setText(bundle.getString("Modelling")); // NOI18N
    radioButtonPanel.add(modellingRadioButton, new java.awt.GridBagConstraints());

    buttonGroup1.add(operatingRadioButton);
    operatingRadioButton.setSelected(true);
    operatingRadioButton.setText(bundle.getString("Operating")); // NOI18N
    radioButtonPanel.add(operatingRadioButton, new java.awt.GridBagConstraints());

    jPanel1.add(radioButtonPanel);

    jPanel3.setLayout(new java.awt.BorderLayout());

    jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getSize()-1f));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText(bundle.getString("NOTE_PRESSING_CANCEL")); // NOI18N
    jPanel3.add(jLabel1, java.awt.BorderLayout.CENTER);

    jPanel1.add(jPanel3);

    buttonPanel.setLayout(new java.awt.GridBagLayout());

    okButton.setText("OK");
    okButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okButtonActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
    buttonPanel.add(okButton, gridBagConstraints);

    cancelButton.setText(bundle.getString("Cancel")); // NOI18N
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelButtonActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    buttonPanel.add(cancelButton, gridBagConstraints);

    jPanel1.add(buttonPanel);

    getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    modelName = modelsComboBox.getSelectedItem().toString();
    modelingMode = modellingRadioButton.isSelected();
    dispose();
  }//GEN-LAST:event_okButtonActionPerformed

  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    dispose();
  }//GEN-LAST:event_cancelButtonActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.ButtonGroup buttonGroup1;
  private javax.swing.JPanel buttonPanel;
  private javax.swing.JButton cancelButton;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JPanel jPanel3;
  private javax.swing.JRadioButton modellingRadioButton;
  private javax.swing.JComboBox<Object> modelsComboBox;
  private javax.swing.JButton okButton;
  private javax.swing.JRadioButton operatingRadioButton;
  private javax.swing.JPanel radioButtonPanel;
  // End of variables declaration//GEN-END:variables
  // CHECKSTYLE:ON
}
